package kandl.automation.steprunner.selenium;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.TimeUnit;

import kandl.automation.Browser;
import kandl.automation.testsuite.TestProfile;

public class WindowsWebDriverFactory implements WebDriverFactory {

    @Value("${selenium.timeout.seconds}")
    private int timeout;

    public WindowsWebDriverFactory(String ieLocation,
                                   String chromeLocation) {
        System.setProperty("webdriver.ie.driver", ieLocation);
        System.setProperty("webdriver.chrome.driver", chromeLocation);
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public WebDriver getWebDriver(TestProfile profile) {
        Browser browser = profile.getBrowser();
        String type = browser.getType();
        WebDriver driver;
        if (type.equals(Browser.FIREFOX_TYPE)) {
            driver = new FirefoxDriver();
        }
        else if (type.equals(Browser.IE_TYPE1)){
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability("native events", false);
            driver = new InternetExplorerDriver(dc);
            ((JavascriptExecutor)driver).executeScript("window.resizeTo(1920, 1080);");
        }
        else if (type.equals(Browser.IE_TYPE2)){
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability("native events", false);
            driver = new InternetExplorerDriver(dc);
            ((JavascriptExecutor)driver).executeScript("window.resizeTo(1920, 1080);");
        }
        else if (type.equals(Browser.IE_TYPE3)){
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability("native events", false);
            driver = new InternetExplorerDriver(dc);
            ((JavascriptExecutor)driver).executeScript("window.resizeTo(1920, 1080);");
        }
        else if (type.equals(Browser.IE_TYPE4)){
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability("native events", false);
            driver = new InternetExplorerDriver(dc);
            ((JavascriptExecutor)driver).executeScript("window.resizeTo(1920, 1080);");
        }
        else if (type.equals(Browser.CHROME_TYPE)){
            driver = new ChromeDriver();
        }
        else {
            throw new UnsupportedOperationException("Cannot start a Webdriver for " + browser);
        }

        driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        return driver;
    }
}
